package HLCustomUI;

import HLCode.HLObject;
import HLCustom.HLMain;
import HLCustomFarm.HLFarm_H;
import HLLib.base.HLGraphics;
import HLLib.base.HLInputManager;
import HLLib.base.HLInputManager_H;
import HLLib.base.HLMath;
import HLLib.base.HLRectangle;
import HLLib.base.HLScreen;
import HLLib.base.HLString;
import HLLib.ui.HLUI;

public class HLListBoxSeed extends HLObject implements HLListBox_H, HLInputManager_H, HLFarm_H {

    public HLUI ui;

    public HLListBox listBox;

    public int start;

    public int end;

    public int result;

    public int uiSeedType;

    public HLMain main;

    public HLRectangle rectName;

    public HLRectangle rectListBox;

    public HLListBoxSeed() {

    }

    public void Init(int uiSeedType, int startFrame, int endFrame, HLMain main) {
        this.main = main;
        this.uiSeedType = uiSeedType;
        main.uiSeedManager.Load(uiSeedType, -1);
        ui = main.uiSeedManager.CreateUI(uiSeedType);
        start = startFrame;
        end = endFrame;
        ui.curFrameIndex = (short) startFrame;
        int screenWidth = HLScreen.Width();
        int screenHeight = HLScreen.Height();
        SetSize(0, 0, screenWidth, screenHeight);

    }

    public void SetSize(int x, int y, int width, int height) {
        ui.SetRect(x, y, width, height);

        HLRectangle tmp = ui.FindControlArea(11);
        rectName = CloneRect(tmp);

        tmp = ui.FindControlArea(12);
        rectListBox = CloneRect(tmp);

        listBox = new HLListBox();
        //
        ClearListBoxItems();
    }

    public HLRectangle CloneRect(HLRectangle rect) {
        HLRectangle result1 = new HLRectangle();
        result1.x = rect.x;
        result1.y = rect.y;
        result1.width = rect.width;
        result1.height = rect.height;
        return result1;
    }

    public int GetItemWidth() {
        return rectListBox.width;
    }

    public void GetItemHeight() {

    }

    public void Logic() {
        result = 0;
        int id = ui.Logic();
        int i = 0;
        while (i < 4) {
            int ids = HLMath.Add(21, i);
            int idex = HLMath.Add(start, i);
            boolean isideids = HLMath.IsEqual(id, ids);
            boolean isFrameNotIdex = HLMath.IsNotEqual(ui.curFrameIndex, idex);
            if (HLMath.And(isideids, isFrameNotIdex)) {
                ui.curFrameIndex = (short) idex;
                result |= EVENT_CHANGE_PAGE;
                break;
            }
            i++;
        }
        if (HLInputManager.IsInputDown(GAME_BUTTON_LEFT)) {
            if (ui.curFrameIndex > start) {
                ui.PreviousCurFrame();
                result |= EVENT_CHANGE_PAGE;
            }
        }
        if (HLInputManager.IsInputDown(GAME_BUTTON_RIGHT)) {
            if (ui.curFrameIndex < end) {
                ui.NextCurFrame();
                result |= EVENT_CHANGE_PAGE;
            }
        }
        listBox.Logic();
        if (main.buttonManager.TouchByType(BUTTON_CONFIRM)) {
            result |= LIST_BOX_EVENT_ITEM_CLICKED;
        }
        if (result == 0) {
            result |= listBox.result;
        }
    }

    public int GetcurFrame() {
        return ui.curFrameIndex;
    }

    public void ClearListBoxItems() {
        listBox = new HLListBox();
        //
        listBox.y = rectListBox.y;
        listBox.x = rectListBox.x;
        listBox.width = rectListBox.width;
        listBox.height = rectListBox.height;
    }

    public void Render(HLGraphics g) {
        ui.Render(g);
        listBox.Render(g);
        int itemsCount = listBox.items.Count();
        boolean itemsNotNull = HLMath.IsNotNull(listBox.items);
        if (itemsNotNull) {
            if (itemsCount > 0) {

                HLIListItem item = (HLIListItem) listBox.items.GetItem(listBox.selectedIndex);
                HLString name = item.GetName();
                int screenWidth = HLScreen.Width();
                if (name == null) {
                } else {
                    g.DrawStringAdjustable(name, 0xffffffff, 0, 0, rectName.y, screenWidth, rectName.height, TEXT_STYLE_NORMAL, HCENTER_VCENTER);
                }
            }
        }
    }

    public void AddItem(HLIListItem item) {
        listBox.AddItem(item);
    }

    public void AddItemAndId(HLIListItem item, int id) {
        listBox.AddItemAndId(item, id);
    }

    public int GetSelectedId() {
        int id = listBox.GetSelectedId();
        return id;
    }

    public void Refresh() {
        listBox.Refresh();
    }

    public boolean IsClicked() {
        int clickMask = HLMath.AndInt(listBox.result, LIST_BOX_EVENT_ITEM_CLICKED);
        boolean isClick = HLMath.IsNotEqual(clickMask, 0);
        return isClick;
    }

    public void Unload() {
    }
}
